define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index' + location.search,
                    add_url: 'order/order/add',
                    edit_url: 'order/order/edit',
                    del_url: 'order/order/del',
                    multi_url: 'order/order/multi',
                    table: 'spell_order',
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: true,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                // searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'uid', title: __('Uid')},
                        {field: 'model', title: __('Model'), formatter: Table.api.formatter.status,searchList: {"1":"普通单","2":"拼总价","3":"活动拼团"}},
                        {field: 'orderId', title: '订单编号'},
                        {field: 'username', title: __('Username')},
                        {field: 'addrmobile', title: __('Mobile')},
                        {field: 'province', title: __('Province')},
                        {field: 'city', title: __('City')},
                        {field: 'district', title: __('District')},
                        {field: 'address', title: __('Address'),operate:false},
                        // {field: 'fromUid', title: __('Fromuid')},
                       /* {field: 'ptid', title: __('Ptid')},*/
                        // {field: 'orderPrice', title: __('Orderprice'), operate:'BETWEEN'},
                        // {field: 'goodsPrice', title: __('Goodsprice'), operate:'BETWEEN'},
                        {field: 'payPrice', title: __('Payprice'), operate:'BETWEEN'},
                        {field: 'orderStatus', title: __('Orderstatus'), formatter: Table.api.formatter.status,searchList: {"0":"已失效","1":"待支付","2":"已支付","3":"已成团","4":"待发货","5":"已发货","6":"已收货","7":"已回购","9":"已评论","10":"已取消"}},
                        {field: 'payType', title: __('Paytype'),formatter: Table.api.formatter.status,searchList: {"none":"未选择","credit":"余额","wechat":"微信","alipay":"支付宝"}},
                        {field: 'payTime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: '下单时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'expressName', title: __('Expressname'),operate:false},
                        {field: 'expressNo', title: __('Expressno'),operate:false},
                        /*{field: 'finishtime', title: __('Finishtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},*/
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {name: 'detail',  title: '查看SKU', icon: 'fa fa-suitcase', classname: 'btn btn-xs btn-primary btn-browser btn-dialog',url:'order/Spelldetail/index',
                                },
                                {name: 'detail',  title: '发货', icon: 'fa fa-truck', classname: 'btn btn-xs btn-primary btn-ajax btn-detail', url: 'order/order/shipments',
                                    visible:function (row) {

                                        if(row.orderStatus== 4){
                                            return  true;
                                        }
                                        return false;
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            if($('#orderStatus').val()===4){
                var options = table.bootstrapTable('getOptions');
                options.queryParams = function (params) {
                    params.filter = JSON.stringify({orderStatus: 4});
                    return params;
                };
            }
            // 为表格绑定事件
            Table.api.bindevent(table);


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
